﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSPhuCapKhac : BusinessBase
    {

        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
      
        public void EditPCK(int hdId, decimal heSo, string loaiPhuCap, DateTime ngayHieuLuc, DateTime ngayHetHieuLuc, string ghiChu)
        {
            List<NSPhuCapKhac> lst = (List<NSPhuCapKhac>)HttpContext.Current.Session["PCK"];
            NSPhuCapKhac pc = lst.Where(a => a.PhuCapKhacId.Equals(hdId)).SingleOrDefault();
            pc.NgayHieuLuc = ngayHieuLuc;
            pc.NgayHetHieuLuc = ngayHetHieuLuc;
            pc.GhiChu = ghiChu;
            pc.LoaiPhuCapKhac = loaiPhuCap;
            pc.HeSoPhuCap = heSo;
         
        }
        public void DeleletAllPCKByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSPhuCapKhacs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSPhuCapKhacs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void DeletePCK(int id)
        {
            List<NSPhuCapKhac> lst = (List<NSPhuCapKhac>)HttpContext.Current.Session["PCK"];
            NSPhuCapKhac pc = lst.Where(a => a.PhuCapKhacId.Equals(id)).SingleOrDefault();
            lst.Remove(pc);
        }
        public List<NSPhuCapKhac> LoadPCKByNhanVienId(int nhanVienId)
        {
            var query = (from a in db.NSPhuCapKhacs
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.NgayHetHieuLuc descending
                         select a).Take(3).ToList();
            
            return query;
        }
        public void UpdatePCK_NhanVien(int nhanVienId, List<NSPhuCapKhac> pckObject)
        {

            var queryPCKByNhanVienId = (from a in db.NSPhuCapKhacs
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryPCKByNhanVienId.Count > 0)
            {
                db.NSPhuCapKhacs.DeleteAllOnSubmit(queryPCKByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSPhuCapKhac pc in pckObject)
            {
                NSPhuCapKhac item = new NSPhuCapKhac();
                CopyNsPhuCapchucvu(pc, item);
                db.NSPhuCapKhacs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNsPhuCapchucvu(NSPhuCapKhac src, NSPhuCapKhac des)
        {
         
            des.HeSoPhuCap = src.HeSoPhuCap;
            des.NgayHetHieuLuc = src.NgayHetHieuLuc;
            des.NgayHieuLuc = src.NgayHieuLuc;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.LoaiPhuCapKhac = src.LoaiPhuCapKhac;

        }
        public NSPhuCapKhac GetPhuCapKhac(int nhanVienId, decimal heSo, string loaiPhuCap, DateTime ngayHieuLuc, DateTime ngayHetHieuLuc, string ghiChu)
        {
            var db = new DataClassesDataContext();
            NSPhuCapKhac hd = new NSPhuCapKhac();

            hd.PhuCapKhacId = 0 - DateTime.Now.Second;
            hd.NgayHieuLuc = ngayHieuLuc;
            hd.NgayHetHieuLuc = ngayHetHieuLuc;
            hd.GhiChu = ghiChu;
            hd.LoaiPhuCapKhac = loaiPhuCap;
            hd.HeSoPhuCap = heSo;
         
            hd.NhanVienID = nhanVienId;
            return hd;
        }
    }

}